const wx2my = require('../../wx2my');

// pages/user_center/user_center.js
var http = require("../../utils/http.js");

var http = require("../../utils/http.js");

var util = require("../../utils/util.js");

const app = getApp();
Page({
  data: {
    hava_mag: true,
    no_msg: true,
    corp_id: 0,
    collectionCount: 0,
    GouWuCheCount: 0,
    adminid: 0
  },
  onLoad: function (e) {
    let that = this;
    let msgToken = wx2my.getStorageSync('token');

    if (msgToken == '' || msgToken == null) {
      that.setData({
        no_msg: false
      });
    } else {
      that.setData({
        hava_mag: false
      });
    }
  },
  onReady: function () {
    let that = this;
    app.wxRequest('customer', {}, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        that.setData({
          tel: res.data.data.tel
        });
      }
    });
  },
  onShow: function () {
    this.get_msg();
    this.showCollectionCount(); //  let data = http.getCartCount();//重新计算购物车总数量

    var that = this;
    var params = {
      url: "/p/shopCart/prodCount",
      method: "GET",
      data: {},
      callBack: function (res) {
        if (res > 0) {
          that.setData({
            GouWuCheCount: res
          });
        } else {
          that.setData({
            GouWuCheCount: 0
          });
        }
      }
    };
    http.request(params); //  console.log(data)
  },

  openPrivacyContract() {
    wx.openPrivacyContract({
      success: res => {
        console.log('openPrivacyContract success');
      },
      fail: res => {
        console.error('openPrivacyContract fail', res);
      }
    });
  },
  // 跳转核销
  HeXiao() {
    wx2my.navigateTo({
      url: '/pages/HeXiao/HeXiao'
    });
  },
  // 跳转购卡
  TurnBuyCard(){
    wx2my.navigateTo({
      url: '/pages/buyCard/buyCard'
    });
  },
  /**
   * 我的收藏跳转
   */
  myCollectionHandle: function () {
    var url = '/pages/prod-classify/prod-classify?sts=5';
    var id = 0;
    var title = "我的收藏商品";

    if (id) {
      url += "&tagid=" + id + "&title=" + title;
    }

    wx2my.navigateTo({
      url: url
    });
  },

  /**
   * 购物车跳转
   */
  toGouWuChe: function () {
    var url = '/pages/basket/basket'; //  var id = 0;
    //  var title = "我的收藏商品";
    //  if (id) {
    //    url += "&tagid=" + id + "&title=" + title;
    //  }

    wx2my.navigateTo({
      url: url
    });
  },
  showCollectionCount: function () {
    var ths = this;
    wx2my.showLoading();
    var params = {
      url: "/p/user/collection/count",
      method: "GET",
      data: {},
      callBack: function (res) {
        wx2my.hideLoading();
        ths.setData({
          collectionCount: res
        });
      }
    };
    http.request(params);
  },
  // 授权登陆
  getUserProfile: function (e) {
    let that = this;
    app.fm.wx_login((flag, userInfo) => {
      if (flag == 2) {
        that.setData({
          no_msg: true,
          hava_mag: false
        });
        that.get_msg();
      } else {}
    }, app);
  },

  get_msg() {
    let that = this;
    app.wxRequest('profile', {
      'token': wx2my.getStorageSync('token')
    }, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        that.setData({
          msg: res.data.data,
          corp_id: res.data.data.controller_id,
          adminid: res.data.data.adminid
        });
      }
    });
  },

  tocall() {
    let that = this;
    wx2my.showModal({
      title: '温馨提示',
      content: '拨打电话:' + that.data.tel,

      success(res) {
        if (res.confirm) {
          wx2my.makePhoneCall({
            phoneNumber: that.data.tel
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }

    });
  },

  change() {
    wx2my.navigateTo({
      url: '/pages/person_detail/person_detail'
    });
  },

  login(e) {
    console.log('已接收消息1');
    wx.requestSubscribeMessage({
      tmplIds: ['JAjsx51BAgLHQSjSnw0jvF_ZeIobi7eqKHdrFg6z_dU'],

      success(res) {
        console.log(res);

        if (res['JAjsx51BAgLHQSjSnw0jvF_ZeIobi7eqKHdrFg6z_dU'] === 'accept') {
          console.log('用户同意了');
        }
      },

      fail(err) {
        //失败
        console.error(err);
        reject();
      }

    }); // console.log(e)
    // let that = this;
    // app.wxRequest('shop_login',{
    //   'adminid':that.data.adminid
    // },function(res){
    //   console.log(res)
    //   if(res.data.code  == 1){
    //     app.fm.save('shop_token', res.data.data.shop_token);
    //     if (res.data.data.type == 1 ) {//代理商
    //       wx.navigateTo({
    //         url: '/pages/agent_index/agent_index?type='+1,
    //       });
    //     }else if(res.data.data.type == 2){//场地商
    //       wx.navigateTo({
    //         url: '/pages/agent_index/agent_index?type='+2,
    //       });
    //     }
    //   }
    // })
  },

  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {}
});